package com.iotimc.attendancesystem.server.model;

import android.text.TextUtils;
import android.util.Log;

import com.iotimc.attendancesystem.dao.GreenDaoManager;
import com.iotimc.attendancesystem.dao.PersonEntityDao;
import com.iotimc.attendancesystem.entity.PersonEntity;
import com.pefis.natives.Ast2600JNI;
import com.pefis.natives.PfsBioInfo;

import java.util.Date;
import java.util.List;

/**
 * @author david
 * @date 2017/12/20
 */

public class PersonEntityModel {
    private static final String TAG = "PersonEntityModel";

    PersonEntityDao personEntityDao;
    static PersonEntityModel model;

    public static PersonEntityModel getInstance() {
        if (model == null) {
            model = new PersonEntityModel();
        }
        return model;
    }

    private PersonEntityModel() {
        personEntityDao = GreenDaoManager.getInstance().getSession().getPersonEntityDao();
    }

    public void insertOrReplace(List<PersonEntity> personEntities) {
        for (PersonEntity entity : personEntities) {
            if (!TextUtils.isEmpty(entity.getCode())) {
                PersonEntity localEntity = personEntityDao.queryBuilder().
                        where(PersonEntityDao.Properties.Code.eq(entity.getCode())).unique();
                byte[] imageCode = entity.getImageCode();
                //本地有相应的数据，执行修改操作
                if (localEntity != null) {
                    localEntity.setAdmin(entity.getAdmin());
                    localEntity.setName(entity.getName() + "1");
                    localEntity.setSex(entity.getSex());
                    localEntity.setCard(entity.getCard());
                    localEntity.setPosition(entity.getPosition());
                    localEntity.setUpdateTime(new Date());
                    localEntity.setImage(entity.getImage());
                    if (imageCode != null) {
                        localEntity.setImageCode(imageCode);
                        boolean check = checkHeadImage(localEntity);
                        if (check) {
                            personEntityDao.insertOrReplace(localEntity);
                        } else {
                            Log.i(TAG, "insertOrReplace: " + localEntity.getCode() + ":同步人员信息失败");
                        }
                    } else {
                        personEntityDao.insertOrReplace(localEntity);
                    }
                }
                //本地没有对应数据
                else {
                    entity.setICodeId(0);
                    if (entity.getImageCode() != null) {
                        boolean check = checkHeadImage(entity);
                        if (check) {
                            personEntityDao.insertOrReplace(entity);
                        } else {
                            Log.i(TAG, "insertOrReplace: " + entity.getCode() + ":同步人员信息失败");
                        }
                    } else {
                        personEntityDao.insertOrReplace(entity);
                    }

                }
            }

        }
    }

    private boolean checkHeadImage(PersonEntity personEntity) {
        int iCodeId;
        Log.i(TAG, "checkHeadImage: personEntity.getICodeId()" + personEntity.getICodeId());
        if (personEntity.getICodeId() != 0) {
            iCodeId = personEntity.getICodeId();
        } else {
            String columnName = PersonEntityDao.Properties.ICodeId.columnName;
            //查找人脸最后的id
            List<PersonEntity> personImageEntities = personEntityDao.
                    queryRaw(" where " + columnName + " = (select max(" + columnName + ") " +
                            "from " + PersonEntityDao.TABLENAME + ")");

            if (null == personImageEntities || personImageEntities.size() == 0) {
                iCodeId = 1;
            } else {
                iCodeId = (personImageEntities.get(0).getICodeId() + 1);
            }
        }
        PfsBioInfo.getInstance().mEnrollID = iCodeId;
        Log.i(TAG, "保存文件 ID = " + PfsBioInfo.getInstance().mEnrollID);
        int[] dup_id = new int[1];
        Ast2600JNI
                .getInstance()
                .pisFcInfraredLedControl(
                        PfsBioInfo.getInstance().mContextID,
                        PfsBioInfo.PFSBIO_IR_LED_LOW_ON);

        int vnRet = Ast2600JNI
                .getInstance()
                .pisFcCheckDuplicate(
                        PfsBioInfo.getInstance().mContextID,
                        personEntity.getImageCode(), dup_id);
        Log.i(TAG, PfsBioInfo.getInstance().mEnrollID + "   检测重复建模  返回特征码ID:" + dup_id[0] + "返回结果  " + vnRet);
        //
        if (dup_id[0] != PfsBioInfo.getInstance().mEnrollID) {
            if (vnRet != PfsBioInfo.PFSBIO_OK) {
                Log.i(TAG, "checkHeadImage: 该人脸已经存在!");
                return false;
            }
        }

        vnRet = Ast2600JNI
                .getInstance()
                .pisFcSetAtTptArray(
                        PfsBioInfo.getInstance().mContextID,
                        PfsBioInfo.getInstance().mEnrollID,
                        personEntity.getImageCode());
        if (vnRet != 0) {
            //设置模型失败
            Log.i(TAG, "checkHeadImage: 设置模型失败!");
            return false;
        }
        personEntity.setICodeId(iCodeId);
        return true;
    }
}
